<!-- 车辆列表 -->
<template>
  <div class="dashboard-container vehicle customer-list-box ">
    <!-- 搜索 -->
    <el-card style="height:80px;margin-bottom:20px" shadow="never">
      <el-form label-width="80px" style="l" :model="vehicleForm">
        <el-row type="flex">
          <el-col style="height:40px;padding:0 30px" :span="8">
            <el-form-item label="车辆类型">
              <el-select v-model="vehicleForm.truckTypeId">
                <el-option
                  v-for="item in getCarTypeList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="车辆号码">
              <el-input
                placeholder="请输入车牌号"
                style="width:280px"
                v-model="vehicleForm.licensePlate"
              >
                <el-option></el-option>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-button type="danger" style="width:90px" @click="searchBtn"
              >搜索</el-button
            >
            <el-button style="width:90px" @click="clearBtn">重置</el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <!-- 导航 -->
    <!-- <el-card style="height:48px"> -->
    <el-menu
      mode="horizontal"
      active-text-color="#e15536"
      default-active="null"
      style="margin-bottom:20px"
    >
      <el-menu-item
        style="width:120px;text-align:center"
        index="null"
        @click="clearBtn"
        >全部{{ +carStatus['0'] + +carStatus['1'] }}</el-menu-item
      >
      <el-menu-item
        style="width:120px;text-align:center"
        index="1"
        @click="statysCar(0)"
        >可用{{ carStatus['0'] }}</el-menu-item
      >
      <el-menu-item
        style="width:120px;text-align:center"
        index="0"
        @click="statysCar(1)"
        >停用{{ carStatus['1'] }}</el-menu-item
      >
    </el-menu>
    <!-- </el-card> -->
    <!-- 表格区域 -->
    <el-card>
      <!-- 新增按钮 -->
      <el-button type="danger" style="margin-bottom:20px" @click="showAdd"
        >新增车辆</el-button
      >
      <el-table :data="tabelList">
        <el-table-column label="序号" width="60" prop="ind"></el-table-column>
        <el-table-column
          label="车牌号码"
          width="115"
          prop="licensePlate"
        ></el-table-column>
        <el-table-column
          label="车辆类型"
          width="111"
          prop="truckTypeName"
        ></el-table-column>
        <el-table-column label="司机数量" width="111"></el-table-column>
        <el-table-column label="车辆状态" width="111">
          <template #default="{row}">
            <div style="display:flex; align-items: center;">
              <span
                v-if="row.workStatus === 1"
                style="width:6px;height:6px;background-color: green;margin-right:6px;border-radius:50%;"
              ></span>
              <span
                v-else
                style="width:6px;height:6px;background-color: #e15536;margin-right:6px;border-radius:50%;"
              ></span>
              <span>{{ row.workStatus === 0 ? '停用' : '启用' }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="GPS设备ID"
          width="111"
          prop="deviceGpsId"
        ></el-table-column>
        <el-table-column
          label="实载重量(T)"
          width="167"
          prop="allowableLoad"
        ></el-table-column>
        <el-table-column
          label="实载体积(方)"
          width="167"
          prop="allowableVolume"
        ></el-table-column>
        <el-table-column label="操作" width="220" header-align="center">
          <template #default="{row}">
            <el-button type="text" size="mini" @click="jopDetail(row.id)"
              >查看详情</el-button
            >
            <el-button
              v-if="row.workStatus !== 0"
              type="text"
              @click="truckStatusShop(row)"
              style="color:red"
              size="mini"
              >停用</el-button
            >
            <el-button v-else type="text" @click="truckStatus(row)" size="mini"
              >启用</el-button
            >
            <el-button type="text" @click="driverBtn(row)" size="mini"
              >配置司机</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-row type="flex" justify="center" style="margin:20px;font-size:20px">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="params.page"
          :page-size="params.pageSize"
          :page-sizes="[10, 20, 30, 50]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
        >
        </el-pagination>
      </el-row>
    </el-card>
    <!-- 添加弹窗 -->
    <vehicle-add
      :showDialog="showDialog"
      @closeDialog="closeDialog"
      :tabelList="getCarTypeList"
    ></vehicle-add>

    <!-- 配置司机确认弹窗 -->
    <el-dialog :visible.sync="showCarDialog" title="配置司机" width="640px">
      <el-descriptions :column="2">
        <el-descriptions-item label="车牌号">{{
          carSee.licensePlate
        }}</el-descriptions-item>
        <el-descriptions-item label="车型">
          {{ carSee.truckTypeName }}
        </el-descriptions-item>
        <el-descriptions-item label="车辆状态">
          <template>
            <div
              style="text-align: center;
              line-height: 20px;
              width: 41px;
              height: 20px;
              border-radius: 13.5px;
              color: #fff;
              font-size: 12px;
              background: #37c608;"
            >
              可用
            </div>
          </template>
        </el-descriptions-item>
        <el-descriptions-item label="车实载重量">{{
          carSee.allowableLoad
        }}</el-descriptions-item>
        <el-descriptions-item label="实载体积">{{
          carSee.allowableVolume
        }}</el-descriptions-item>
      </el-descriptions>
      <el-row type="flex" justify="start" align="center">
        <p>配置司机：</p>
        <el-select
          style="width:340px"
          v-model="carAdd.driverIds"
          allow-create
          filterable
          multiple
          placeholder="请输入司机名称"
          @change="changeSle"
        >
          <el-option
            v-for="item in carList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-row>
      <el-table :data="carTableList" style="margin-bottom:20px">
        <el-table-column label="序号">
          <template #default="{row}">
            <span>{{ row.indexCar }}</span>
          </template>
        </el-table-column>
        <el-table-column label="司机名称">
          <template #default="{row}">
            <span>{{ row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="2" label="操作">
          <template #default="{row}">
            <el-row>
              <el-button type="text" size="mini" @click="seeCarPeople(row.id)"
                >查看</el-button
              >
              <el-button type="text" size="mini" @click="delCarTabel(row)"
                >删除</el-button
              >
            </el-row>
          </template>
        </el-table-column>
        <template #empty>
          <el-empty description="这里空空如也"></el-empty>
        </template>
      </el-table>
      <el-row type="flex" justify="center" style="margin-buttom:20px">
        <el-button type="danger" style="width:90px" @click="isOkcar"
          >确认</el-button
        >
        <el-button style="width:90px" @click="showCarDialog = false"
          >取消</el-button
        >
      </el-row>
    </el-dialog>
  </div>
</template>
<script>
import {
  truckList,
  getCarTypeList,
  truckStatusUse,
  truckStatusNoUse,
  truckTypeDetail,
  unBindDriverList,
  getCarTypeStatics,
  driverArrangeCar
} from '@/api/transit'
import VehicleAdd from './components/vehicle-add.vue'
import vehicleAdd from './components/vehicle-add.vue'
export default {
  components: { vehicleAdd },
  computeds: {
    VehicleAdd
  },
  name: 'Vehicle',
  data() {
    return {
      // 配置司机中表单
      carTableList: [],
      // 车辆状态 数量
      carStatus: {},
      // 配置司机中的详情
      carSee: {},
      // 配置司机数据
      carAdd: {
        driverIds: [],
        truckId: ''
      },
      // 司机列表
      carList: [],
      // 车辆状态是否启用
      status: '',
      // 配置司机功能弹框
      showCarDialog: false,
      // 配置司机弹窗
      showDriver: false,
      // 当前操作的id
      currendtID: '',
      // 启用弹窗
      showTruck: false,
      // 新增弹窗
      showDialog: false,
      // 搜索表单
      vehicleForm: {
        licensePlate: '',
        truckTypeId: ''
      },
      getCarTypeList: [],
      // 导航
      isCollapse: true,
      // 表格数据
      tabelList: [],
      // 页数数据
      params: {
        page: 1,
        pageSize: 10
      },
      // 总条数
      total: 0
    }
  },
  async created() {
    this.getDriverList(this.params)
    // 获取车辆类型列表
    const result = await getCarTypeList()
    // console.log(result)
    this.getCarTypeList = result.data
    // 司机列表
    const res = await unBindDriverList()
    // console.log(res)
    this.carList = res.data
    // 车辆数量
    const res2 = await getCarTypeStatics()
    // console.log(res2.data)
    this.carStatus = res2.data
  },
  methods: {
    // 查看
    seeCarPeople(id) {
      this.$router.push({
        path: 'vehicle-detail',
        query: { id: this.currendtIDD, defaultSee: 'second' }
      })
    },
    // 配置司机表格删除按钮
    delCarTabel(row) {
      console.log(row)
      this.carAdd.driverIds.filter((item, index) => {
        return item === row.id
      })
      this.carAdd.driverIds = [
        ...this.carAdd.driverIds.filter((item, index) => {
          return item !== row.id
        })
      ]
      this.carTableList = []
      this.carList.forEach((item, index) => {
        this.carAdd.driverIds.forEach(key => {
          if (key === item.id) {
            this.$set(item, 'indexCar', this.carTableList.length + 1)
            this.carTableList.push(item)
          }
        })
      })
      console.log(this.carTableList)
    },
    // 点击详情按钮
    jopDetail(id) {
      console.log(id)
      this.$router.push({
        path: `vehicle-detail`,
        query: { id: id, defaultSee: 'first' }
      })
    },
    // 配置司机最后的确认按钮
    async isOkcar() {
      const res = await driverArrangeCar(this.carAdd)
      if (res.code !== 200) {
        this.$message.error(res.msg)
      } else {
        this.$message.success('配置成功')
      }
      this.showCarDialog = false
    },
    // 下拉列表
    changeSle() {
      if (this.carAdd.driverIds.length > 2) {
        this.$message.error('车辆最多只能绑定2名司机，请先删除绑定司机！')
        this.carAdd.driverIds.pop()
      } else {
        this.carTableList = []
        this.carList.forEach((item, index) => {
          this.carAdd.driverIds.forEach(key => {
            if (key === item.id) {
              this.$set(item, 'indexCar', this.carTableList.length + 1)
              this.carTableList.push(item)
            }
          })
        })
      }
    },
    //, 全部按钮
    statysCar(workStatus = '') {
      this.params.page = 1
      this.params.pageSize = 10
      this.params.workStatus = workStatus
      this.getDriverList(this.params)
    },
    // 搜索按钮
    searchBtn() {
      // console.log(this.vehicleForm.licensePlate)
      if (!this.vehicleForm.licensePlate && !this.vehicleForm.truckTypeId) {
        return
      }
      this.params = {
        page: 1,
        pageSize: 10
      }
      this.getDriverList({ ...this.params, ...this.vehicleForm })
    },
    // 重置按钮
    clearBtn() {
      // 清空数据
      this.vehicleForm = {
        licensePlate: '',
        truckTypeId: ''
      }
      this.params = {
        page: 1,
        pageSize: 10
      }
      //重新获取数据
      this.getDriverList(this.params)
    },
    // 获取车辆数据列表
    async getDriverList(data = {}) {
      // console.log(data)
      const res = await truckList(data)
      // console.log(res.data)
      this.total = +res.data.counts
      this.tabelList = res.data.items
      // 序号
      let sum = this.params.page
      if (sum > 1) {
        sum = sum - 1
        sum = sum * 10 * (this.params.pageSize / 10)
        sum++
      }
      // 防止丢失响应式
      if (this.tabelList) {
        this.tabelList.forEach(item => {
          this.$set(item, 'ind', sum++)
        })
      }
    },
    //页数发生变化
    handleCurrentChange(page) {
      this.params.page = page
      this.getDriverList(this.params)
    },
    // 下拉列数发生变化
    handleSizeChange(pagesize) {
      this.params.pageSize = pagesize
      this.getDriverList(this.params)
    },
    // 接受子组件的通知关闭弹窗
    closeDialog(value) {
      this.showDialog = false
      if (value) this.getDriverList(this.params)
    },
    // 新增车辆
    showAdd() {
      this.showDialog = true
    },
    // 点击启用按钮
    truckStatus(data) {
      const h = this.$createElement
      this.$msgbox({
        title: '车辆启用',
        message: h('p', null, [
          h('p', null, '确定要启用吗？车辆启用需满足以下条件：'),
          h('span', { style: 'margin-left:10px' }, '1.车辆信息以完善 '),
          h('span', { style: 'margin-left:50px' }, '2.绑定司机>=2，且有排版')
        ]),
        showCancelButton: true,
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        beforeClose: (action, instance, done) => {
          done()
        }
      })
        .then(async action => {
          const res = await truckStatusUse(data.id)
          //     // this.showTruck = false
          this.$message.error(res.msg)
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },
    // 停用
    truckStatusShop(data) {
      const h = this.$createElement
      this.$msgbox({
        title: '车辆停用',
        message: h('p', null, [
          h('p', null, '确定要停用吗？车辆停用需满足以下条件：'),
          h('span', { style: 'margin-left:10px' }, '1.车辆当前无运输任务 '),
          h('span', { style: 'margin-left:50px' }, '2.车辆无关联运输线路'),
          h(
            'p',
            {
              style:
                'color: rgb(255, 179, 2);margin-top: 10px;margin-bottom: -10px;'
            },
            '注：停用后将自动接触绑定'
          )
        ]),
        showCancelButton: true,
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        beforeClose: (action, instance, done) => {
          done()
        }
      })
        .then(async action => {
          const res = await truckStatusNoUse(data.id)
          //     // this.showTruck = false
          this.$message.error(res.msg)
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },
    // 车辆配置按钮
    driverBtn(data) {
      this.currendtIDD = data.id
      const h = this.$createElement
      this.$msgbox({
        title: '车辆配置',
        message: h('p', null, [
          h('p', null, '配置司机需要满足以下条件：'),
          h('span', { style: 'margin-left:10px' }, '1.车辆信息以完善 '),
          h('span', { style: 'margin-left:50px' }, '2.车辆无未完成运输任务')
        ]),
        showCancelButton: true,
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        beforeClose: (action, instance, done) => {
          done()
        }
      })
        .then(async action => {
          //     // this.showTruck = false
          this.showCarDialog = true
          // 调用详情列表
          const res = await truckTypeDetail(data.id)
          console.log(res)
          this.carSee = res.data
          // console.log(this.carSee)
          // 获取车辆id
          this.carAdd.truckId = data.id
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    }
  }
}
</script>
<style scoped></style>
<style rel="stylesheet/scss" lang="scss" scoped>
.customer-list-box {
  padding: 23px 20px 20px;
  // justify-content: space-between;
}
.alert {
  margin: 10px 0px;
}
.pagination {
  margin-top: 10px;
}
.vehicle ::v-deep .el-table td,
.el-table th {
  padding: 12px 0;
  min-width: 0;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  text-overflow: ellipsis;
  vertical-align: middle;
  position: relative;
  text-align: left;
  overflow: hidden;
}

.vehicle {
  ::v-deep .el-dialog__title {
    width: 73px;
    height: 25px;
    font-size: 18px;
    font-family: PingFangSC, PingFangSC-Regular;
    font-weight: 400;
    text-align: left;
    color: #20232a;
    line-height: 25px;
    letter-spacing: 0px;
  }
  ::v-deep .el-dialog__body {
    text-align: center;
    padding: 20px 20px 0px 20px;
  }
  .warn-img {
    width: 40px;
  }
  p {
    text-align: left;
    font-size: 16px;
    font-weight: 400;
  }
}

// 下拉框
::v-deep .el-select {
  width: 100%;
}
</style>

<style>
.startconfirm {
  width: 481px;
}
.startconfirm .el-message-box__header {
  padding-bottom: 4px;
}
.startconfirm .el-message-box__container {
  margin-bottom: 15px;
}
.confirm .el-message-box__status.el-icon-warning {
  left: 70px;
}
.confirm.el-message-box__title {
  font-size: 16px;
}
.confirm .el-message-box__message p {
  color: #f92d2d;
  margin-top: 5px;
}
.confirm .el-message-box__btns {
  text-align: center;
  margin-top: 10px;
}
</style>
